<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 6.3.0">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">


<link rel="stylesheet" href="/lib/font-awesome/css/all.min.css">

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"chuangges.gitee.io","root":"/","scheme":"Pisces","version":"7.8.0","exturl":false,"sidebar":{"position":"right","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":true,"show_result":true,"style":"mac"},"back2top":{"enable":true,"sidebar":false,"scrollpercent":false},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":false,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},"path":"search.xml"};
  </script>

  <meta name="description" content="基础入门、渲染模式、服务端渲染原理">
<meta property="og:type" content="article">
<meta property="og:title" content="最强首屏渲染方案 NextJS">
<meta property="og:url" content="https://chuangges.gitee.io/2023/09/04/23-next%E9%A6%96%E5%B1%8F%E6%B8%B2%E6%9F%93/index.html">
<meta property="og:site_name" content="闯哥要去流浪了">
<meta property="og:description" content="基础入门、渲染模式、服务端渲染原理">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2023-09-04T07:41:02.000Z">
<meta property="article:modified_time" content="2023-09-20T02:02:11.312Z">
<meta property="article:author" content="chuangges">
<meta property="article:tag" content="前端技术栈">
<meta name="twitter:card" content="summary">

<link rel="canonical" href="https://chuangges.gitee.io/2023/09/04/23-next%E9%A6%96%E5%B1%8F%E6%B8%B2%E6%9F%93/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>

  <title>最强首屏渲染方案 NextJS | 闯哥要去流浪了</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

<link rel="alternate" href="/atom.xml" title="闯哥要去流浪了" type="application/atom+xml">
</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">闯哥要去流浪了</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
      <p class="site-subtitle" itemprop="description">愿你岁月可回首，且以深情共白头</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="main-menu menu">
        <li class="menu-item menu-item-home">

    <a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签</a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a>

  </li>
  </ul>
</nav>




</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>
  <div class="reading-progress-bar"></div>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://chuangges.gitee.io/2023/09/04/23-next%E9%A6%96%E5%B1%8F%E6%B8%B2%E6%9F%93/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/chuang.png">
      <meta itemprop="name" content="chuangges">
      <meta itemprop="description" content="chuangge's blog.">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="闯哥要去流浪了">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          最强首屏渲染方案 NextJS
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2023-09-04 15:41:02" itemprop="dateCreated datePublished" datetime="2023-09-04T15:41:02+08:00">2023-09-04</time>
            </span>
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="far fa-calendar-check"></i>
                </span>
                <span class="post-meta-item-text">更新于</span>
                <time title="修改时间：2023-09-20 10:02:11" itemprop="dateModified" datetime="2023-09-20T10:02:11+08:00">2023-09-20</time>
              </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/%E5%89%8D%E7%AB%AF%E6%8A%80%E6%9C%AF%E6%A0%88/" itemprop="url" rel="index"><span itemprop="name">前端技术栈</span></a>
                </span>
            </span>

          <br>
            <span class="post-meta-item" title="本文字数">
              <span class="post-meta-item-icon">
                <i class="far fa-file-word"></i>
              </span>
                <span class="post-meta-item-text">本文字数：</span>
              <span>20k</span>
            </span>
            <span class="post-meta-item" title="阅读时长">
              <span class="post-meta-item-icon">
                <i class="far fa-clock"></i>
              </span>
                <span class="post-meta-item-text">阅读时长 &asymp;</span>
              <span>18 分钟</span>
            </span>
            <div class="post-description">基础入门、渲染模式、服务端渲染原理</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <h1 id="一、基础入门"><a href="#一、基础入门" class="headerlink" title="一、基础入门"></a>一、基础入门</h1><h2 id="核心机制"><a href="#核心机制" class="headerlink" title="核心机制"></a>核心机制</h2><blockquote>
<p>不需要配置 webpack 构建方案，可通过简单配置去优化打包性能，构建时会输出页面资源的信息，如果需要分析具体组成则可以使用 @next&#x2F;bundle-analyzer。</p>
</blockquote>
<ul>
<li>babel 内置，支持 JS 代码向后兼容。</li>
<li>postcss 内置，支持 CSS 代码向后兼容。</li>
<li>browserslist 支持配置兼容的浏览器信息。</li>
<li>eslint 选择使用，检测代码格式和自定义规则。</li>
<li>prettier 可扩展使用，格式化代码和自定义规则。</li>
<li>TypeScript 选择使用，保证代码质量和可维护性。</li>
<li>css modules 内置：命名 [name].module.css。</li>
<li>styled-jsx 内置：支持 React 组件中编写 css。</li>
<li>tailwind css 可扩展使用：不用命名的原子化样式。</li>
<li>以下为打包优化功能：</li>
<li>tree shaking：消除无用 js。</li>
<li>代码压缩：默认开启 gzip 压缩。</li>
<li>页面静态化：将动态内容预渲染为静态页面。</li>
<li>异步动态加载组件：功能同 React.lazy。</li>
<li>按需打包第三方包：设置 transpilePackages。</li>
</ul>
<h2 id="构建项目"><a href="#构建项目" class="headerlink" title="构建项目"></a>构建项目</h2>  <figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 脚手架构建</span></span><br><span class="line">npm install -g yarn</span><br><span class="line">yarn create next-app --typescript</span><br><span class="line">create-next-app next-demo</span><br><span class="line">cd next-demo、next dev</span><br><span class="line"><span class="comment">// package.json 配置：localhost:3000</span></span><br><span class="line"><span class="attr">scripts</span>: &#123;</span><br><span class="line">    <span class="string">&quot;dev&quot;</span>: <span class="string">&quot;next dev&quot;</span>,</span><br><span class="line">    <span class="string">&quot;build&quot;</span>: <span class="string">&quot;next build&quot;</span>,</span><br><span class="line">    <span class="string">&quot;start&quot;</span>: <span class="string">&quot;next start&quot;</span>,</span><br><span class="line">    <span class="string">&quot;lint&quot;</span>: <span class="string">&quot;next lint&quot;</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">// 手动构建</span></span><br><span class="line">mkdir next-demo</span><br><span class="line">cd next-demo</span><br><span class="line">npm init -y</span><br><span class="line">yarn add react react-dom next</span><br><span class="line">mkdir pages、mkdir components</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">// 目录结构</span></span><br><span class="line">|-- .<span class="property">gitignore</span></span><br><span class="line">|-- package.<span class="property">json</span></span><br><span class="line">|-- yarn.<span class="property">lock</span></span><br><span class="line">|-- layouts         <span class="comment">// 布局组件</span></span><br><span class="line">|   |-- layout.<span class="property">js</span></span><br><span class="line">|-- components      <span class="comment">// 组件文件夹</span></span><br><span class="line">|   |-- nav.<span class="property">js</span></span><br><span class="line">|-- pages</span><br><span class="line">|   |-- index.<span class="property">js</span> </span><br><span class="line">|   |-- _error.<span class="property">js</span>   <span class="comment">// 错误文件</span></span><br><span class="line">|   |-- _app.<span class="property">js</span>     <span class="comment">// 全局布局组件入口</span></span><br><span class="line">|-- <span class="keyword">static</span>          <span class="comment">// 静态资源文件夹，编译打包不被执行</span></span><br><span class="line">|-- public</span><br><span class="line">|-- favicon.<span class="property">ico</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">// next.config.js：配置文件</span></span><br><span class="line"><span class="keyword">const</span> withCss = <span class="built_in">require</span>(<span class="string">&#x27;@zeit/next-css&#x27;</span>)</span><br><span class="line"><span class="keyword">const</span> configs = &#123;</span><br><span class="line">    <span class="attr">distDir</span>: <span class="string">&#x27;dest&#x27;</span>,       <span class="comment">// 输出目录</span></span><br><span class="line">    <span class="attr">generateEtags</span>: <span class="literal">true</span>,   <span class="comment">// 是否每个路由生成 Etag</span></span><br><span class="line">    <span class="attr">onDemandEntries</span>: &#123;     <span class="comment">// 本地开发时页面缓存：时长、页面数</span></span><br><span class="line">        <span class="attr">maxInactiveAge</span>: <span class="number">25</span> * <span class="number">1000</span>,</span><br><span class="line">        <span class="attr">pagesBufferLength</span>: <span class="number">2</span></span><br><span class="line">    &#125;,</span><br><span class="line">    <span class="attr">pageExtensions</span>: [<span class="string">&#x27;jsx&#x27;</span>, <span class="string">&#x27;js&#x27;</span>],  <span class="comment">// pages 目录作为页面解析的后缀</span></span><br><span class="line">    <span class="attr">generateBuildId</span>: <span class="keyword">async</span> () =&gt; &#123;  <span class="comment">// 配置 buildId：默认 unique-id</span></span><br><span class="line">        <span class="keyword">if</span> (process.<span class="property">env</span>.<span class="property">YOUR_BUILD_ID</span>) &#123;</span><br><span class="line">            <span class="keyword">return</span> process.<span class="property">env</span>.<span class="property">YOUR_BUILD_ID</span></span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> <span class="literal">null</span></span><br><span class="line">    &#125;,</span><br><span class="line">    <span class="title function_">webpack</span>(<span class="params">config, options</span>) &#123;     <span class="comment">// 手动修改 webpack 配置</span></span><br><span class="line">        <span class="keyword">return</span> config</span><br><span class="line">    &#125;,</span><br><span class="line">    <span class="title function_">webpackDevMiddleware</span>(<span class="params">config</span>) &#123;  <span class="comment">// 手动修改 webpackDevMiddleware 配置</span></span><br><span class="line">        <span class="keyword">return</span> config</span><br><span class="line">    &#125;,</span><br><span class="line">    <span class="attr">env</span>: &#123;                  <span class="comment">// 页面可通过 process.env.customkey 获取 value</span></span><br><span class="line">        <span class="attr">customkey</span>: <span class="string">&#x27;value&#x27;</span></span><br><span class="line">    &#125;,</span><br><span class="line">    <span class="comment">// 下面两个要通过引入 import getConfig from &#x27;next/config&#x27; 来读取</span></span><br><span class="line">    <span class="attr">serverRuntimeConfig</span>: &#123;    <span class="comment">// 只有在服务端渲染时才会获取的配置</span></span><br><span class="line">        <span class="attr">mySecret</span>: <span class="string">&#x27;secret&#x27;</span>,</span><br><span class="line">        <span class="attr">secondSecret</span>: process.<span class="property">env</span>.<span class="property">SECOND_SECRET</span></span><br><span class="line">    &#125;,</span><br><span class="line">    <span class="attr">publicRuntimeConfig</span>: &#123;    <span class="comment">// 在服务端渲染和客户端渲染都可获取的配置</span></span><br><span class="line">        <span class="attr">staticFolder</span>: <span class="string">&#x27;/static&#x27;</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">if</span> (<span class="keyword">typeof</span> <span class="built_in">require</span> !== <span class="string">&#x27;undefined&#x27;</span>) &#123;</span><br><span class="line">    <span class="built_in">require</span>.<span class="property">extensions</span>[<span class="string">&#x27;.css&#x27;</span>] = <span class="function"><span class="params">file</span> =&gt;</span> &#123;&#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="variable language_">module</span>.<span class="property">exports</span> = <span class="title function_">withCss</span>(configs)   <span class="comment">// 集成 css 的配置</span></span><br></pre></td></tr></table></figure>


<h2 id="集成-Node"><a href="#集成-Node" class="headerlink" title="集成 Node"></a>集成 Node</h2>  <figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// server.js：作为 koa 中间件使用</span></span><br><span class="line"><span class="keyword">const</span> <span class="title class_">Koa</span> = <span class="built_in">require</span>(<span class="string">&#x27;koa&#x27;</span>)</span><br><span class="line"><span class="keyword">const</span> <span class="title class_">Router</span> = <span class="built_in">require</span>(<span class="string">&#x27;koa-router&#x27;</span>)</span><br><span class="line"><span class="keyword">const</span> next = <span class="built_in">require</span>(<span class="string">&#x27;next&#x27;</span>)</span><br><span class="line"><span class="keyword">const</span> dev = process.<span class="property">env</span>.<span class="property">NODE_ENV</span> !== <span class="string">&#x27;production&#x27;</span></span><br><span class="line"><span class="keyword">const</span> app = <span class="title function_">next</span>(&#123; dev &#125;)</span><br><span class="line"><span class="keyword">const</span> handle = app.<span class="title function_">getRequestHandler</span>()</span><br><span class="line">app.<span class="title function_">prepare</span>().<span class="title function_">then</span>(<span class="function">() =&gt;</span> &#123;     <span class="comment">// pages 目录编译后启动服务</span></span><br><span class="line">    <span class="keyword">const</span> server = <span class="keyword">new</span> <span class="title class_">Koa</span>()</span><br><span class="line">    <span class="keyword">const</span> router = <span class="keyword">new</span> <span class="title class_">Router</span>()</span><br><span class="line">    <span class="comment">// /a/1 格式的路由代理到 /a?id=1，这样就不会 404</span></span><br><span class="line">    router.<span class="title function_">get</span>(<span class="string">&#x27;/a/:id&#x27;</span>, <span class="keyword">async</span> ctx =&gt; &#123;</span><br><span class="line">        <span class="keyword">const</span> id = ctx.<span class="property">params</span>.<span class="property">id</span></span><br><span class="line">        <span class="keyword">await</span> <span class="title function_">handle</span>(ctx.<span class="property">req</span>, ctx.<span class="property">res</span>, &#123;<span class="attr">pathname</span>: <span class="string">&#x27;/a&#x27;</span>, <span class="attr">query</span>: &#123;id&#125; &#125;)</span><br><span class="line">        ctx.<span class="property">respond</span> = <span class="literal">false</span></span><br><span class="line">    &#125;)</span><br><span class="line">    server.<span class="title function_">use</span>(router.<span class="title function_">routes</span>())</span><br><span class="line">    server.<span class="title function_">use</span>(<span class="keyword">async</span> (ctx, next) =&gt; &#123;</span><br><span class="line">        <span class="keyword">await</span> <span class="title function_">handle</span>(ctx.<span class="property">req</span>, ctx.<span class="property">res</span>)</span><br><span class="line">        ctx.<span class="property">respond</span> = <span class="literal">false</span></span><br><span class="line">    &#125;)</span><br><span class="line">    server.<span class="title function_">listen</span>(<span class="number">3000</span>, <span class="function">() =&gt;</span> &#123;</span><br><span class="line">        <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">`Server on http://localhost:3000`</span>)</span><br><span class="line">    &#125;)</span><br><span class="line">&#125;)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">// server.js：作为 express 中间件使用</span></span><br><span class="line"><span class="keyword">const</span> express = <span class="built_in">require</span>(<span class="string">&#x27;express&#x27;</span>)</span><br><span class="line"><span class="keyword">const</span> next = <span class="built_in">require</span>(<span class="string">&#x27;next&#x27;</span>)</span><br><span class="line"><span class="keyword">const</span> dev = process.<span class="property">env</span>.<span class="property">NODE_ENV</span> !== <span class="string">&#x27;production&#x27;</span></span><br><span class="line"><span class="keyword">const</span> app = <span class="title function_">next</span>(&#123; dev &#125;)</span><br><span class="line"><span class="keyword">const</span> handle = app.<span class="title function_">getRequestHandler</span>()</span><br><span class="line">app.<span class="title function_">prepare</span>().<span class="title function_">then</span>(<span class="function">() =&gt;</span> &#123;</span><br><span class="line">    <span class="keyword">const</span> server = <span class="title function_">express</span>()</span><br><span class="line">    <span class="comment">// post 页面路由覆盖处理：req、res、实际跳转页面、路由参数</span></span><br><span class="line">    server.<span class="title function_">get</span>(<span class="string">&#x27;/p/:id&#x27;</span>, <span class="function">(<span class="params">req, res</span>) =&gt;</span> &#123;</span><br><span class="line">        app.<span class="title function_">render</span>(req, res, <span class="string">&#x27;/post&#x27;</span>, &#123;<span class="attr">title</span>: req.<span class="property">params</span>.<span class="property">id</span>&#125;)</span><br><span class="line">    &#125;)</span><br><span class="line">    <span class="comment">// 所有请求都交由next处理</span></span><br><span class="line">    server.<span class="title function_">get</span>(<span class="string">&#x27;*&#x27;</span>, <span class="function">(<span class="params">req, res</span>) =&gt;</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="title function_">handle</span>(req, res)</span><br><span class="line">    &#125;)</span><br><span class="line">    <span class="comment">// 监听端口号</span></span><br><span class="line">    server.<span class="title function_">listen</span>(<span class="number">3000</span>, <span class="function">(<span class="params">err</span>) =&gt;</span> &#123;</span><br><span class="line">        <span class="keyword">if</span> (err) <span class="keyword">throw</span> err</span><br><span class="line">        <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&#x27;Server on http://localhost:3000&#x27;</span>)</span><br><span class="line">    &#125;)</span><br><span class="line">&#125;)</span><br><span class="line">.<span class="title function_">catch</span>(<span class="function"><span class="params">err</span> =&gt;</span> &#123;</span><br><span class="line">    <span class="variable language_">console</span>.<span class="title function_">error</span>(err.<span class="property">stack</span>)</span><br><span class="line">    process.<span class="title function_">exit</span>(<span class="number">1</span>)</span><br><span class="line">&#125;)</span><br><span class="line"></span><br><span class="line"><span class="comment">// package.json 配置</span></span><br><span class="line"><span class="attr">scripts</span>: &#123; <span class="string">&quot;dev&quot;</span>: <span class="string">&quot;node server.js&quot;</span> &#125;</span><br></pre></td></tr></table></figure>


<h2 id="样式方案"><a href="#样式方案" class="headerlink" title="样式方案"></a>样式方案</h2>  <figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 1、next 默认方案 styled-jsx：全局属性 global 去掉后为组件样式</span></span><br><span class="line"><span class="keyword">const</span> <span class="title function_">A</span> = (<span class="params">&#123; name &#125;</span>) =&gt; (</span><br><span class="line">    <span class="language-xml"><span class="tag">&lt;&gt;</span></span></span><br><span class="line"><span class="language-xml">        &#123;/* <span class="tag">&lt;<span class="name">span</span> <span class="attr">class</span>=<span class="string">&quot;jsx-3081 link&quot;</span>&gt;</span>渲染后<span class="tag">&lt;/<span class="name">span</span>&gt;</span> */&#125;</span></span><br><span class="line"><span class="language-xml">        <span class="tag">&lt;<span class="name">span</span> <span class="attr">className</span>=<span class="string">&quot;link&quot;</span>&gt;</span>A页面<span class="tag">&lt;/<span class="name">span</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">        <span class="tag">&lt;<span class="name">style</span> <span class="attr">jsx</span> <span class="attr">global</span>&gt;</span><span class="language-css">&#123;` <span class="selector-class">.link</span> &#123; <span class="attribute">color</span>: red; &#125; `&#125;</span><span class="tag">&lt;/<span class="name">style</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">    <span class="tag">&lt;/&gt;</span></span></span><br><span class="line">)</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> A</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@title</span> 2、css 集成方案：next 默认不支持 import css 文件</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> 根目录新增配置文件：next.config.js</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> 安装插件：npm install <span class="doctag">@zeit</span>/next-css --save </span></span><br><span class="line"><span class="comment">**/</span></span><br><span class="line"><span class="keyword">const</span> withCss = <span class="built_in">require</span>(<span class="string">&#x27;@zeit/next-css&#x27;</span>)</span><br><span class="line"><span class="keyword">if</span> (<span class="keyword">typeof</span> <span class="built_in">require</span> !== <span class="string">&#x27;undefined&#x27;</span>) &#123;</span><br><span class="line">    <span class="built_in">require</span>.<span class="property">extensions</span>[<span class="string">&#x27;.css&#x27;</span>]= <span class="function"><span class="params">file</span> =&gt;</span> &#123;&#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="variable language_">module</span>.<span class="property">exports</span> = <span class="title function_">withCss</span>(&#123;&#125;)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">// 3、styled-component 方案：安装配置省略</span></span><br><span class="line"><span class="keyword">import</span> styled <span class="keyword">from</span> <span class="string">&#x27;styled-components&#x27;</span></span><br><span class="line"><span class="keyword">const</span> <span class="title class_">Title</span> = styled.<span class="property">h1</span><span class="string">`color: yellow;font-size: 40px;`</span></span><br><span class="line"><span class="keyword">const</span> <span class="title function_">A</span> = (<span class="params">&#123; name &#125;</span>) =&gt; (<span class="language-xml"><span class="tag">&lt;&gt;</span><span class="tag">&lt;<span class="name">Title</span>&gt;</span>A 页面<span class="tag">&lt;/<span class="name">Title</span>&gt;</span><span class="tag">&lt;/&gt;</span></span>)</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> A</span><br></pre></td></tr></table></figure>


<h2 id="公共组件"><a href="#公共组件" class="headerlink" title="公共组件"></a>公共组件</h2>  <figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// components/Header.js：导航组件</span></span><br><span class="line"><span class="keyword">import</span> <span class="title class_">Link</span> <span class="keyword">from</span> <span class="string">&#x27;next/link&#x27;</span></span><br><span class="line"><span class="keyword">const</span> linkStyle = &#123; <span class="attr">marginRight</span>: <span class="number">15</span> &#125;</span><br><span class="line"><span class="keyword">const</span> <span class="title function_">Header</span> = (<span class="params"></span>) =&gt; (</span><br><span class="line">    <span class="language-xml"><span class="tag">&lt;<span class="name">div</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">        <span class="tag">&lt;<span class="name">Link</span> <span class="attr">href</span>=<span class="string">&quot;/&quot;</span>&gt;</span><span class="tag">&lt;<span class="name">a</span> <span class="attr">style</span>=<span class="string">&#123;linkStyle&#125;</span>&gt;</span>Home<span class="tag">&lt;/<span class="name">a</span>&gt;</span><span class="tag">&lt;/<span class="name">Link</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">        <span class="tag">&lt;<span class="name">Link</span> <span class="attr">href</span>=<span class="string">&quot;/about&quot;</span>&gt;</span><span class="tag">&lt;<span class="name">a</span> <span class="attr">style</span>=<span class="string">&#123;linkStyle&#125;</span>&gt;</span>About<span class="tag">&lt;/<span class="name">a</span>&gt;</span><span class="tag">&lt;/<span class="name">Link</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">    <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span></span><br><span class="line">)</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> <span class="title class_">Header</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">// components/Layout.js：布局组件</span></span><br><span class="line"><span class="keyword">import</span> <span class="title class_">Header</span> <span class="keyword">from</span> <span class="string">&#x27;./Header&#x27;</span></span><br><span class="line"><span class="keyword">const</span> layoutStyle = &#123;</span><br><span class="line">    <span class="attr">margin</span>: <span class="number">20</span>,</span><br><span class="line">    <span class="attr">padding</span>: <span class="number">20</span>,</span><br><span class="line">    <span class="attr">border</span>: <span class="string">&#x27;1px solid #DDD&#x27;</span></span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">const</span> <span class="title function_">Layout</span> = (<span class="params">props</span>) =&gt; (</span><br><span class="line">    <span class="language-xml"><span class="tag">&lt;<span class="name">div</span> <span class="attr">style</span>=<span class="string">&#123;layoutStyle&#125;</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">          <span class="tag">&lt;<span class="name">Header</span> /&gt;</span></span></span><br><span class="line"><span class="language-xml">          &#123;props.children&#125;</span></span><br><span class="line"><span class="language-xml">    <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span></span><br><span class="line">)</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> <span class="title class_">Layout</span></span><br></pre></td></tr></table></figure>

<h2 id="动态页面"><a href="#动态页面" class="headerlink" title="动态页面"></a>动态页面</h2>  <figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// pages/index.js：项目首页</span></span><br><span class="line"><span class="keyword">import</span> <span class="title class_">Layout</span> <span class="keyword">from</span> <span class="string">&#x27;../components/Layout&#x27;</span></span><br><span class="line"><span class="keyword">import</span> <span class="title class_">Link</span> <span class="keyword">from</span> <span class="string">&#x27;next/link&#x27;</span></span><br><span class="line"><span class="keyword">const</span> <span class="title function_">PostLink</span> = (<span class="params">props</span>) =&gt; (</span><br><span class="line">  <span class="language-xml"><span class="tag">&lt;<span class="name">li</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">    <span class="tag">&lt;<span class="name">Link</span> <span class="attr">as</span>=<span class="string">&#123;</span>`/<span class="attr">p</span>/$&#123;<span class="attr">props.id</span>&#125;`&#125; <span class="attr">href</span>=<span class="string">&#123;</span>`/<span class="attr">post</span>?<span class="attr">title</span>=<span class="string">$&#123;props.title&#125;</span>`&#125;&gt;</span></span></span><br><span class="line"><span class="language-xml">        <span class="tag">&lt;<span class="name">a</span>&gt;</span>&#123;props.title&#125;<span class="tag">&lt;/<span class="name">a</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">    <span class="tag">&lt;/<span class="name">Link</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">  <span class="tag">&lt;/<span class="name">li</span>&gt;</span></span></span><br><span class="line">)</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> () =&gt; (</span><br><span class="line">    <span class="language-xml"><span class="tag">&lt;<span class="name">Layout</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">        <span class="tag">&lt;<span class="name">h1</span>&gt;</span>My Blog<span class="tag">&lt;/<span class="name">h1</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">        <span class="tag">&lt;<span class="name">ul</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">            <span class="tag">&lt;<span class="name">PostLink</span> <span class="attr">id</span>=<span class="string">&quot;hello&quot;</span> <span class="attr">title</span>=<span class="string">&quot;Hello Next.js&quot;</span>/&gt;</span></span></span><br><span class="line"><span class="language-xml">            <span class="tag">&lt;<span class="name">PostLink</span> <span class="attr">id</span>=<span class="string">&quot;learn&quot;</span> <span class="attr">title</span>=<span class="string">&quot;Learn Next.js&quot;</span>/&gt;</span></span></span><br><span class="line"><span class="language-xml">            <span class="tag">&lt;<span class="name">PostLink</span> <span class="attr">id</span>=<span class="string">&quot;deploy&quot;</span> <span class="attr">title</span>=<span class="string">&quot;Deploy apps&quot;</span>/&gt;</span></span></span><br><span class="line"><span class="language-xml">        <span class="tag">&lt;/<span class="name">ul</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">    <span class="tag">&lt;/<span class="name">Layout</span>&gt;</span></span></span><br><span class="line">)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">// pages/post.js：文章页面</span></span><br><span class="line"><span class="keyword">import</span> <span class="title class_">Layout</span> <span class="keyword">from</span> <span class="string">&#x27;../components/Layout&#x27;</span></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> (props) =&gt; (</span><br><span class="line">    <span class="language-xml"><span class="tag">&lt;<span class="name">Layout</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">        <span class="tag">&lt;<span class="name">h1</span>&gt;</span>&#123;props.url.query.title&#125;<span class="tag">&lt;/<span class="name">h1</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">        <span class="tag">&lt;<span class="name">p</span>&gt;</span>This is the blog post content.<span class="tag">&lt;/<span class="name">p</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">    <span class="tag">&lt;/<span class="name">Layout</span>&gt;</span></span></span><br><span class="line">)</span><br></pre></td></tr></table></figure>


<h2 id="路由机制"><a href="#路由机制" class="headerlink" title="路由机制"></a>路由机制</h2><blockquote>
<p>Link 组件核心是如果其子节点是一个没有 href 属性的 a 标签则渲染为 a 标签并新增 href 属性和重写 onClink，该标签被点击时先执行 onClink 并通过 e.preventDefault() 禁用默认跳转事件，然后切换页面并重新渲染。Link 组件的 useEffect 生命周期和 onMouseEnter 事件中会执行 prefetch，核心是通过 <code>&lt;link rel=&quot;preload&quot;&gt;</code> 对跳转页面的资源进行预加载。</p>
</blockquote>
  <figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// a 页面：Link 组件(子元素用一个标签并且不可设置样式)、Router 模块</span></span><br><span class="line"><span class="keyword">import</span> <span class="title class_">Link</span> <span class="keyword">from</span> <span class="string">&#x27;next/link&#x27;</span></span><br><span class="line"><span class="keyword">import</span> &#123; <span class="title class_">Button</span> &#125; <span class="keyword">from</span> <span class="string">&#x27;antd&#x27;</span></span><br><span class="line"><span class="keyword">import</span> <span class="title class_">Router</span> <span class="keyword">from</span> <span class="string">&#x27;next/router&#x27;</span></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> () =&gt; &#123;</span><br><span class="line">    <span class="keyword">const</span> <span class="title function_">goB</span> = (<span class="params"></span>) =&gt; &#123;</span><br><span class="line">        <span class="title class_">Router</span>.<span class="title function_">push</span>(<span class="string">&#x27;/b?id=2&#x27;</span>)</span><br><span class="line">        <span class="title class_">Router</span>.<span class="title function_">push</span>(&#123; <span class="attr">pathname</span>: <span class="string">&#x27;/b&#x27;</span>, <span class="attr">query</span>: &#123;<span class="attr">id</span>: <span class="number">2</span>&#125; &#125;)</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> (</span><br><span class="line">        <span class="language-xml"><span class="tag">&lt;&gt;</span></span></span><br><span class="line"><span class="language-xml">            <span class="tag">&lt;<span class="name">Link</span> <span class="attr">href</span>=<span class="string">&quot;/a?id=1&quot;</span> <span class="attr">as</span>=<span class="string">&quot;/a/1&quot;</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">            <span class="tag">&lt;<span class="name">Link</span> <span class="attr">href</span>=<span class="string">&#123;&#123;pathname:</span> &#x27;/<span class="attr">a</span>&#x27;, <span class="attr">query:</span> &#123;<span class="attr">id:</span> <span class="attr">1</span>&#125;&#125;&#125;&gt;</span></span></span><br><span class="line"><span class="language-xml">                <span class="tag">&lt;<span class="name">a</span> <span class="attr">style</span>=<span class="string">&#123;&#123;color:</span> &#x27;<span class="attr">red</span>&#x27;&#125;&#125;&gt;</span>跳转地址为 as 形式<span class="tag">&lt;/<span class="name">a</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">            <span class="tag">&lt;/<span class="name">Link</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">            <span class="tag">&lt;<span class="name">Button</span> <span class="attr">onClick</span>=<span class="string">&#123;goB&#125;</span>&gt;</span>跳转到 b<span class="tag">&lt;/<span class="name">Button</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">        <span class="tag">&lt;/&gt;</span></span></span><br><span class="line"><span class="language-xml">    )</span></span><br><span class="line"><span class="language-xml">&#125;</span></span><br><span class="line"><span class="language-xml"></span></span><br><span class="line"><span class="language-xml"></span></span><br><span class="line"><span class="language-xml">// b 页面</span></span><br><span class="line"><span class="language-xml">import &#123; withRouter &#125; from &#x27;next/router&#x27;</span></span><br><span class="line"><span class="language-xml">const B = props =&gt; <span class="tag">&lt;<span class="name">span</span>&gt;</span>参数：&#123;props.router.query.id&#125;<span class="tag">&lt;/<span class="name">span</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">export default withRouter(B)</span></span><br></pre></td></tr></table></figure>


<h2 id="异步加载"><a href="#异步加载" class="headerlink" title="异步加载"></a>异步加载</h2>  <figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 异步加载模块</span></span><br><span class="line"><span class="keyword">import</span> moment <span class="keyword">from</span> <span class="string">&#x27;moment&#x27;</span></span><br><span class="line"><span class="keyword">import</span> styled <span class="keyword">from</span> <span class="string">&#x27;styled-components&#x27;</span></span><br><span class="line"><span class="keyword">const</span> <span class="title class_">Title</span> = styled.<span class="property">h1</span><span class="string">`color: yellow; font-size: 40px;`</span></span><br><span class="line"><span class="keyword">const</span> <span class="title function_">A</span> = (<span class="params">&#123; name &#125;</span>) =&gt; &#123;</span><br><span class="line">    <span class="keyword">return</span> (<span class="language-xml"><span class="tag">&lt;&gt;</span><span class="tag">&lt;<span class="name">Title</span>&gt;</span>A 页面的时间差：&#123;time&#125;<span class="tag">&lt;/<span class="name">Title</span>&gt;</span><span class="tag">&lt;/&gt;</span></span>)</span><br><span class="line">&#125;</span><br><span class="line">A.<span class="property">getInitialProps</span> = <span class="keyword">async</span> ctx =&gt; &#123;</span><br><span class="line">    <span class="keyword">const</span> moment = <span class="keyword">await</span> <span class="title function_">import</span>(<span class="string">&#x27;moment&#x27;</span>)</span><br><span class="line">    <span class="keyword">const</span> time = moment.<span class="title function_">default</span>(<span class="title class_">Date</span>.<span class="title function_">now</span>() - <span class="number">60</span> * <span class="number">1000</span>).<span class="title function_">fromNow</span>()</span><br><span class="line">    <span class="keyword">return</span> &#123; time &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> A</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">// 异步加载组件</span></span><br><span class="line"><span class="keyword">import</span> dynamic <span class="keyword">from</span> <span class="string">&#x27;next/dynamic&#x27;</span></span><br><span class="line"><span class="keyword">const</span> <span class="title class_">Comp</span> = <span class="title function_">dynamic</span>(<span class="title function_">import</span>(<span class="string">&#x27;../components/Comp&#x27;</span>))</span><br><span class="line"><span class="keyword">const</span> <span class="title function_">A</span> = (<span class="params">&#123; name, timeDiff &#125;</span>) =&gt; &#123;</span><br><span class="line">    <span class="keyword">return</span> (<span class="language-xml"><span class="tag">&lt;&gt;</span><span class="tag">&lt;<span class="name">Comp</span> /&gt;</span><span class="tag">&lt;/&gt;</span></span>)</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> A</span><br></pre></td></tr></table></figure>


<h2 id="数据获取"><a href="#数据获取" class="headerlink" title="数据获取"></a>数据获取</h2>  <figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 1、客户端渲染 CSR</span></span><br><span class="line"><span class="keyword">import</span> &#123; useEffect, useState &#125; <span class="keyword">from</span> <span class="string">&#x27;react&#x27;</span></span><br><span class="line"><span class="keyword">function</span> <span class="title function_">MyCom</span>(<span class="params"></span>) &#123;</span><br><span class="line">    <span class="keyword">const</span> [data, setData] = <span class="title function_">useState</span>(<span class="literal">null</span>)</span><br><span class="line">    <span class="title function_">useEffect</span>(<span class="function">() =&gt;</span> &#123;</span><br><span class="line">        <span class="title function_">fetch</span>(<span class="string">&#x27;/api/data&#x27;</span>)</span><br><span class="line">        .<span class="title function_">then</span>(<span class="function"><span class="params">res</span> =&gt;</span> res.<span class="title function_">json</span>())</span><br><span class="line">        .<span class="title function_">then</span>(<span class="function"><span class="params">data</span> =&gt;</span> <span class="title function_">setData</span>(data))</span><br><span class="line">    &#125;, [])</span><br><span class="line">    <span class="keyword">return</span> (<span class="language-xml"><span class="tag">&lt;<span class="name">div</span>&gt;</span>&#123;data.name&#125;<span class="tag">&lt;/<span class="name">div</span>&gt;</span></span>)</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@title</span> 2、SWR 钩子：提供了重新验证、间隔更新、缓存等功能的一组 React Hooks。</span></span><br><span class="line"><span class="comment"> * 页面渲染前获取数据时推荐 getServerSideProps，客户端获取数据时则可使用 SWR。</span></span><br><span class="line"><span class="comment">**/</span></span><br><span class="line"><span class="keyword">import</span> useSWR <span class="keyword">from</span> <span class="string">&#x27;swr&#x27;</span>;</span><br><span class="line"><span class="keyword">import</span> axios <span class="keyword">from</span> <span class="string">&quot;axios&quot;</span>;</span><br><span class="line"><span class="keyword">const</span> <span class="title function_">fetcher</span> = url =&gt; axios.<span class="title function_">get</span>(url).<span class="title function_">then</span>(<span class="function"><span class="params">r</span> =&gt;</span> r.<span class="property">data</span>);</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> <span class="keyword">function</span> <span class="title function_">Users</span>(<span class="params"></span>) &#123;</span><br><span class="line">    <span class="keyword">const</span> &#123; </span><br><span class="line">        data,          <span class="comment">// 返回的数据</span></span><br><span class="line">        error,         <span class="comment">// 抛出的错误</span></span><br><span class="line">        isLoading,     <span class="comment">// 是否有正在进行的请求</span></span><br><span class="line">        isValidating,  <span class="comment">// 是否有请求或重新验证</span></span><br><span class="line">        mutate         <span class="comment">// 更改缓存数据的函数</span></span><br><span class="line">    &#125; = <span class="title function_">useSWR</span>(<span class="string">&#x27;/api/users&#x27;</span>, fetcher, options)</span><br><span class="line">    <span class="keyword">if</span> (error) <span class="keyword">return</span> <span class="language-xml"><span class="tag">&lt;<span class="name">div</span>&gt;</span>failed to load<span class="tag">&lt;/<span class="name">div</span>&gt;</span></span></span><br><span class="line">    <span class="keyword">if</span> (!data) <span class="keyword">return</span> <span class="language-xml"><span class="tag">&lt;<span class="name">div</span>&gt;</span>loading...<span class="tag">&lt;/<span class="name">div</span>&gt;</span></span></span><br><span class="line">    <span class="keyword">return</span> <span class="language-xml"><span class="tag">&lt;<span class="name">div</span>&gt;</span>hello &#123;data.name&#125;!<span class="tag">&lt;/<span class="name">div</span>&gt;</span></span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@title</span> 3、服务端渲染 SSR</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> getInitialProps：访问页面时在服务端执行、跳转页面时则会在客户端执行。</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> getServerSideProps：SSR 页面每次请求时都会在服务器重新加载动态页面。</span></span><br><span class="line"><span class="comment"> * </span></span><br><span class="line"><span class="comment"> * <span class="doctag">@despt</span> 两个接口都只能用于 pages 页面并将返回值作为 props 传入，不可用于组件。 </span></span><br><span class="line"><span class="comment"> * <span class="doctag">@despt</span> Next 9.3 废弃了由于执行环境不同而有时需要兼容处理的 getInitialProps。</span></span><br><span class="line"><span class="comment">**/</span></span><br><span class="line"><span class="keyword">import</span> fetch <span class="keyword">from</span> <span class="string">&#x27;isomorphic-unfetch&#x27;</span>   <span class="comment">// 请求外部数据的 axios 类似插件。</span></span><br><span class="line"><span class="keyword">const</span> <span class="title function_">A</span> = (<span class="params">&#123; name &#125;</span>) =&gt; (</span><br><span class="line">    <span class="language-xml"><span class="tag">&lt;&gt;</span><span class="tag">&lt;<span class="name">title</span>&gt;</span>&#123;name&#125;<span class="tag">&lt;/<span class="name">title</span>&gt;</span><span class="tag">&lt;/&gt;</span></span></span><br><span class="line">)</span><br><span class="line">A.<span class="property">getStaticProps</span> = <span class="function">() =&gt;</span> &#123; </span><br><span class="line">    <span class="keyword">return</span> &#123; <span class="attr">name</span>: <span class="string">&#x27;ssh&#x27;</span> &#125;</span><br><span class="line">&#125;</span><br><span class="line">A.<span class="property">getServerSideProps</span> = <span class="keyword">async</span> () =&gt; &#123;  </span><br><span class="line">    <span class="keyword">const</span> res = <span class="keyword">await</span> <span class="title function_">fetch</span>(<span class="string">&#x27;https://api.tvmaze.com/?id=1&#x27;</span>)</span><br><span class="line">    <span class="keyword">const</span> data = <span class="keyword">await</span> res.<span class="title function_">json</span>()</span><br><span class="line">    <span class="keyword">return</span> &#123; <span class="attr">name</span>: data.<span class="property">name</span> &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> A</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@title</span> 4、静态预渲染 SSG</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> getStaticProps：构建时在服务器执行一次，fallback: !false 也会执行。</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> getStaticPaths：根据传入参数为 getStaticProps 动态构建多个静态页面。</span></span><br><span class="line"><span class="comment">**/</span></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">async</span> <span class="keyword">function</span> <span class="title function_">getStaticPaths</span>(<span class="params"></span>) &#123;</span><br><span class="line">    <span class="keyword">const</span> res = <span class="keyword">await</span> <span class="title function_">fetch</span>(<span class="string">&#x27;https://.../posts&#x27;</span>)</span><br><span class="line">    <span class="keyword">const</span> posts = <span class="keyword">await</span> res.<span class="title function_">json</span>()</span><br><span class="line">    <span class="keyword">const</span> paths = posts.<span class="title function_">map</span>(<span class="function">(<span class="params">post</span>) =&gt;</span> (&#123;</span><br><span class="line">        <span class="attr">params</span>: &#123; <span class="attr">id</span>: post.<span class="property">id</span> &#125;</span><br><span class="line">    &#125;))</span><br><span class="line">    <span class="keyword">return</span> &#123; paths, <span class="attr">fallback</span>: <span class="literal">false</span> &#125;    <span class="comment">// false 时其它路由都会返回 404。</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>


<h2 id="扩展功能"><a href="#扩展功能" class="headerlink" title="扩展功能"></a>扩展功能</h2>  <figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@title</span> <span class="variable">Ant</span>-Design 按需加载</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> 安装依赖：npm install babel-plugin-import --save</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> 配置文件：.babelrc、pages/_app.js、pages/index.js </span></span><br><span class="line"><span class="comment">**/</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="string">&quot;presets&quot;</span>: [<span class="string">&quot;next/babel&quot;</span>],  <span class="comment">// 总配置文件，继承 Next 所有配置</span></span><br><span class="line">    <span class="string">&quot;plugins&quot;</span>: [                <span class="comment">// 增加新插件支持按需引入</span></span><br><span class="line">        [<span class="string">&quot;import&quot;</span>, &#123; <span class="string">&quot;libraryName&quot;</span>: <span class="string">&quot;antd&quot;</span> &#125;]</span><br><span class="line">    ]</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> <span class="title class_">App</span> <span class="keyword">from</span> <span class="string">&#x27;next/app&#x27;</span></span><br><span class="line"><span class="keyword">import</span> <span class="string">&#x27;antd/dist/antd.css&#x27;</span></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> <span class="title class_">App</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> <span class="title class_">React</span> <span class="keyword">from</span> <span class="string">&#x27;react&#x27;</span></span><br><span class="line"><span class="keyword">import</span> <span class="title class_">Head</span> <span class="keyword">from</span> <span class="string">&#x27;next/head&#x27;</span></span><br><span class="line"><span class="keyword">import</span> &#123;<span class="title class_">Button</span>&#125; <span class="keyword">from</span> <span class="string">&#x27;antd&#x27;</span>    <span class="comment">// 按需引入了 button 按钮</span></span><br><span class="line"><span class="keyword">const</span> <span class="title function_">Home</span> = (<span class="params"></span>) =&gt; (</span><br><span class="line">    <span class="language-xml"><span class="tag">&lt;<span class="name">div</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">        <span class="tag">&lt;<span class="name">Head</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">            <span class="tag">&lt;<span class="name">title</span>&gt;</span>Home<span class="tag">&lt;/<span class="name">title</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">            <span class="tag">&lt;<span class="name">link</span> <span class="attr">rel</span>=<span class="string">&quot;icon&quot;</span> <span class="attr">href</span>=<span class="string">&quot;/favicon.ico&quot;</span> /&gt;</span></span></span><br><span class="line"><span class="language-xml">        <span class="tag">&lt;/<span class="name">Head</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">        <span class="tag">&lt;<span class="name">Button</span>&gt;</span>按钮<span class="tag">&lt;/<span class="name">Button</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">    <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span></span><br><span class="line">)</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> <span class="title class_">Home</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">// 自定义文档：pages/_document.js</span></span><br><span class="line"><span class="keyword">import</span> <span class="title class_">Document</span>, &#123;<span class="title class_">Html</span>, <span class="title class_">Head</span>, <span class="title class_">Main</span>, <span class="title class_">NextScript</span>&#125; <span class="keyword">from</span> <span class="string">&#x27;next/document&#x27;</span></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> <span class="keyword">class</span> <span class="title class_">MyDocument</span> <span class="keyword">extends</span> <span class="title class_ inherited__">Document</span> &#123;</span><br><span class="line">    <span class="title function_">render</span>(<span class="params"></span>) &#123;</span><br><span class="line">        <span class="keyword">return</span> (</span><br><span class="line">            <span class="language-xml"><span class="tag">&lt;<span class="name">Html</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">                <span class="tag">&lt;<span class="name">Head</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">                    <span class="tag">&lt;<span class="name">title</span>&gt;</span>ssh-next-github<span class="tag">&lt;/<span class="name">title</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">                <span class="tag">&lt;/<span class="name">Head</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">                <span class="tag">&lt;<span class="name">body</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">                    <span class="tag">&lt;<span class="name">Main</span> /&gt;</span></span></span><br><span class="line"><span class="language-xml">                    <span class="tag">&lt;<span class="name">NextScript</span> /&gt;</span></span></span><br><span class="line"><span class="language-xml">                <span class="tag">&lt;/<span class="name">body</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">            <span class="tag">&lt;/<span class="name">Html</span>&gt;</span></span></span><br><span class="line">        )</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@title</span> 自定义 app</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> pages/_app.js：暴露的组件会作为一个全局的包裹组件。</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> 主要功能：固定布局、保持一些公共状态、向页面传入数据。</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> Component：被包裹的页面组件。</span></span><br><span class="line"><span class="comment">**/</span> </span><br><span class="line"><span class="keyword">import</span> <span class="title class_">App</span>, &#123; <span class="title class_">Container</span> &#125; <span class="keyword">from</span> <span class="string">&#x27;next/app&#x27;</span></span><br><span class="line"><span class="keyword">import</span> <span class="string">&#x27;antd/dist/antd.css&#x27;</span></span><br><span class="line"><span class="keyword">import</span> <span class="title class_">React</span> <span class="keyword">from</span> <span class="string">&#x27;react&#x27;</span></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> <span class="keyword">class</span> <span class="title class_">MyApp</span> <span class="keyword">extends</span> <span class="title class_ inherited__">App</span> &#123;</span><br><span class="line">    <span class="title function_">render</span>(<span class="params"></span>) &#123;</span><br><span class="line">        <span class="keyword">const</span> &#123; <span class="title class_">Component</span> &#125; = <span class="variable language_">this</span>.<span class="property">props</span></span><br><span class="line">        <span class="keyword">return</span> (<span class="language-xml"><span class="tag">&lt;<span class="name">Container</span>&gt;</span> <span class="tag">&lt;<span class="name">Component</span> /&gt;</span> <span class="tag">&lt;/<span class="name">Container</span>&gt;</span></span>)</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">// 通过 getInitialProps 获取额外参数</span></span><br><span class="line">    <span class="keyword">static</span> <span class="keyword">async</span> <span class="title function_">getInitialProps</span>(<span class="params">ctx</span>) &#123;</span><br><span class="line">        <span class="keyword">let</span> pageProps = &#123;&#125;</span><br><span class="line">        <span class="keyword">const</span> &#123; <span class="title class_">Component</span> &#125; = ctx</span><br><span class="line">        getInitialPropsif (<span class="title class_">Component</span>.<span class="property">getInitialProps</span>) &#123;</span><br><span class="line">            pageProps = <span class="keyword">await</span> <span class="title class_">Component</span>.<span class="title function_">getInitialProps</span>(ctx)</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> &#123; pageProps &#125;   <span class="comment">// 返回给组件</span></span><br><span class="line">    &#125;</span><br><span class="line">    <span class="title function_">render</span>(<span class="params"></span>) &#123;</span><br><span class="line">        <span class="keyword">const</span> &#123; <span class="title class_">Component</span>, pageProps &#125; = <span class="variable language_">this</span>.<span class="property">props</span></span><br><span class="line">        <span class="keyword">return</span> (<span class="language-xml"><span class="tag">&lt;<span class="name">Container</span>&gt;</span> <span class="tag">&lt;<span class="name">Component</span> &#123;<span class="attr">...pageProps</span>&#125; /&gt;</span> <span class="tag">&lt;/<span class="name">Container</span>&gt;</span></span>)</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>


<h1 id="二、渲染模式"><a href="#二、渲染模式" class="headerlink" title="二、渲染模式"></a>二、渲染模式</h1><h2 id="CSR-客户端渲染"><a href="#CSR-客户端渲染" class="headerlink" title="CSR 客户端渲染"></a>CSR 客户端渲染</h2>  <figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 一般用法：useEffect 请求数据，该动作在页面静态文件加载完毕后执行。</span></span><br><span class="line"><span class="keyword">function</span> <span class="title function_">Profile</span>(<span class="params"></span>) &#123;</span><br><span class="line">    <span class="keyword">const</span> [data, setData] = <span class="title function_">useState</span>(<span class="literal">null</span>)</span><br><span class="line">    <span class="keyword">const</span> [isLoading, setLoading] = <span class="title function_">useState</span>(<span class="literal">false</span>)</span><br><span class="line">    <span class="title function_">useEffect</span>(<span class="function">() =&gt;</span> &#123;</span><br><span class="line">        <span class="title function_">setLoading</span>(<span class="literal">true</span>)</span><br><span class="line">        <span class="title function_">fetch</span>(<span class="string">&#x27;/api/profile-data&#x27;</span>)</span><br><span class="line">        .<span class="title function_">then</span>(<span class="function">(<span class="params">res</span>) =&gt;</span> res.<span class="title function_">json</span>())</span><br><span class="line">        .<span class="title function_">then</span>(<span class="function">(<span class="params">data</span>) =&gt;</span> &#123;</span><br><span class="line">            <span class="title function_">setData</span>(data)</span><br><span class="line">            <span class="title function_">setLoading</span>(<span class="literal">false</span>)</span><br><span class="line">        &#125;)</span><br><span class="line">    &#125;, [])</span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span> (isLoading) <span class="keyword">return</span> <span class="language-xml"><span class="tag">&lt;<span class="name">p</span>&gt;</span>Loading...<span class="tag">&lt;/<span class="name">p</span>&gt;</span></span></span><br><span class="line">    <span class="keyword">if</span> (!data) <span class="keyword">return</span> <span class="language-xml"><span class="tag">&lt;<span class="name">p</span>&gt;</span>No profile data<span class="tag">&lt;/<span class="name">p</span>&gt;</span></span></span><br><span class="line">    <span class="keyword">return</span> (</span><br><span class="line">        <span class="language-xml"><span class="tag">&lt;<span class="name">div</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">            <span class="tag">&lt;<span class="name">h1</span>&gt;</span>&#123;data.name&#125;<span class="tag">&lt;/<span class="name">h1</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">            <span class="tag">&lt;<span class="name">p</span>&gt;</span>&#123;data.bio&#125;<span class="tag">&lt;/<span class="name">p</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">        <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span></span><br><span class="line">    )</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// SWR 类似 React Hook，可用于处理缓存、重新验证、焦点跟踪、间隔重新获取等</span></span><br><span class="line"><span class="keyword">import</span> useSWR <span class="keyword">from</span> <span class="string">&#x27;swr&#x27;</span></span><br><span class="line"><span class="keyword">const</span> <span class="title function_">fetcher</span> = (<span class="params">...args</span>) =&gt; <span class="title function_">fetch</span>(...args).<span class="title function_">then</span>(<span class="function">(<span class="params">res</span>) =&gt;</span> res.<span class="title function_">json</span>())</span><br><span class="line"><span class="keyword">function</span> <span class="title function_">Profile</span>(<span class="params"></span>) &#123;</span><br><span class="line">    <span class="keyword">const</span> &#123; data, error &#125; = <span class="title function_">useSWR</span>(<span class="string">&#x27;/api/profile-data&#x27;</span>, fetcher)</span><br><span class="line">    <span class="keyword">if</span> (error) <span class="keyword">return</span> <span class="language-xml"><span class="tag">&lt;<span class="name">div</span>&gt;</span>Failed to load<span class="tag">&lt;/<span class="name">div</span>&gt;</span></span></span><br><span class="line">    <span class="keyword">if</span> (!data) <span class="keyword">return</span> <span class="language-xml"><span class="tag">&lt;<span class="name">div</span>&gt;</span>Loading...<span class="tag">&lt;/<span class="name">div</span>&gt;</span></span></span><br><span class="line">    <span class="keyword">return</span> (</span><br><span class="line">        <span class="language-xml"><span class="tag">&lt;<span class="name">div</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">            <span class="tag">&lt;<span class="name">h1</span>&gt;</span>&#123;data.name&#125;<span class="tag">&lt;/<span class="name">h1</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">            <span class="tag">&lt;<span class="name">p</span>&gt;</span>&#123;data.bio&#125;<span class="tag">&lt;/<span class="name">p</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">        <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span></span><br><span class="line">    )</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="SSR-服务端渲染"><a href="#SSR-服务端渲染" class="headerlink" title="SSR 服务端渲染"></a>SSR 服务端渲染</h2><blockquote>
<p>SSG HTML 在构建打包时生成并在每次请求中重复使用，SSR HTML 是在每次请求时生成。</p>
</blockquote>
  <figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// getServerSideProps：页面请求时执行，服务端返回最新 html</span></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> <span class="keyword">function</span> <span class="title function_">Page</span>(<span class="params">&#123; data &#125;</span>) &#123;</span><br><span class="line">    <span class="comment">// Render </span></span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">async</span> <span class="keyword">function</span> <span class="title function_">getServerSideProps</span>(<span class="params"></span>) &#123;</span><br><span class="line">    <span class="keyword">const</span> res = <span class="keyword">await</span> <span class="title function_">fetch</span>(<span class="string">`https://../data`</span>)</span><br><span class="line">    <span class="keyword">const</span> data = <span class="keyword">await</span> res.<span class="title function_">json</span>()</span><br><span class="line">    <span class="keyword">return</span> &#123; <span class="attr">props</span>: &#123; data &#125; &#125;  <span class="comment">// 为组件提供动态参数</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>


<h2 id="SSG-静态预渲染"><a href="#SSG-静态预渲染" class="headerlink" title="SSG 静态预渲染"></a>SSG 静态预渲染</h2><blockquote>
<p>解决了页面白屏和 SEO 不友好的问题，但是页面都是以静态资源的方式部署在 CDN，所以只适用于技术文档和官网等静态网页的场景，少量动态数据则可以结合客户端渲染来实现。</p>
</blockquote>
  <figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 静态预渲染：getStaticProps 项目构建时运行，为构建的静态页面传递数据。</span></span><br><span class="line"><span class="keyword">function</span> <span class="title function_">Blog</span>(<span class="params">&#123; posts &#125;</span>) &#123;</span><br><span class="line">    <span class="keyword">return</span> (</span><br><span class="line">        <span class="language-xml"><span class="tag">&lt;<span class="name">ul</span>&gt;</span>&#123;posts.map((post) =&gt; (<span class="tag">&lt;<span class="name">li</span>&gt;</span>&#123;post.title&#125;<span class="tag">&lt;/<span class="name">li</span>&gt;</span>))&#125;<span class="tag">&lt;/<span class="name">ul</span>&gt;</span></span></span><br><span class="line">    )</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">async</span> <span class="keyword">function</span> <span class="title function_">getStaticProps</span>(<span class="params"></span>) &#123;</span><br><span class="line">    <span class="keyword">const</span> res = <span class="keyword">await</span> <span class="title function_">fetch</span>(<span class="string">&#x27;https://.../posts&#x27;</span>)</span><br><span class="line">    <span class="keyword">const</span> posts = <span class="keyword">await</span> res.<span class="title function_">json</span>()</span><br><span class="line">    <span class="keyword">return</span> &#123; <span class="attr">props</span>: &#123; posts &#125; &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> <span class="title class_">Blog</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">// 动态路由预渲染：pages/posts 文件夹创建 [id].js 命名的文件，然后通过 </span></span><br><span class="line"><span class="comment">// getStaticPaths 参数预渲染匹配的多个页面，比如 /posts/1、/posts/2</span></span><br><span class="line">pages/posts/[id].<span class="property">js</span></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">async</span> <span class="keyword">function</span> <span class="title function_">getStaticPaths</span>(<span class="params"></span>) &#123;</span><br><span class="line">    <span class="keyword">const</span> res = <span class="keyword">await</span> <span class="title function_">fetch</span>(<span class="string">&#x27;https://.../posts&#x27;</span>)</span><br><span class="line">    <span class="keyword">const</span> posts = <span class="keyword">await</span> res.<span class="title function_">json</span>()</span><br><span class="line">    <span class="keyword">const</span> paths = posts.<span class="title function_">map</span>(<span class="function">(<span class="params">post</span>) =&gt;</span> (&#123;</span><br><span class="line">        <span class="attr">params</span>: &#123; <span class="attr">id</span>: post.<span class="property">id</span> &#125;</span><br><span class="line">    &#125;))</span><br><span class="line">    <span class="keyword">return</span> &#123; paths, <span class="attr">fallback</span>: <span class="literal">false</span> &#125;  <span class="comment">// 没有匹配到路由返回 404</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="ISR-增量渲染"><a href="#ISR-增量渲染" class="headerlink" title="ISR 增量渲染"></a>ISR 增量渲染</h2><blockquote>
<p>基于 SSG 实现页面请求时重新生成和更新一部分页面，用于页面较多时提高网站响应速度。</p>
</blockquote>
  <figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 生成标记后的 SSG 页面并间隔更新，比如网站新闻</span></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">async</span> <span class="keyword">function</span> <span class="title function_">getStaticProps</span>(<span class="params"></span>) &#123;</span><br><span class="line">    <span class="keyword">const</span> res = <span class="keyword">await</span> <span class="title function_">fetch</span>(<span class="string">&#x27;https://.../posts&#x27;</span>)</span><br><span class="line">    <span class="keyword">const</span> posts = <span class="keyword">await</span> res.<span class="title function_">json</span>()</span><br><span class="line">    <span class="keyword">return</span> &#123;</span><br><span class="line">        <span class="attr">props</span>: &#123; posts, &#125;,</span><br><span class="line">        <span class="attr">revalidate</span>: <span class="number">10</span> <span class="comment">// 接收新请求、每隔十秒钟时重新生成页面</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">async</span> <span class="keyword">function</span> <span class="title function_">getStaticPaths</span>(<span class="params"></span>) &#123;</span><br><span class="line">    <span class="keyword">const</span> res = <span class="keyword">await</span> <span class="title function_">fetch</span>(<span class="string">&#x27;https://.../posts&#x27;</span>)</span><br><span class="line">    <span class="keyword">const</span> posts = <span class="keyword">await</span> res.<span class="title function_">json</span>()</span><br><span class="line">    <span class="keyword">const</span> paths = posts.<span class="title function_">map</span>(<span class="function">(<span class="params">post</span>) =&gt;</span> (&#123;</span><br><span class="line">        <span class="attr">params</span>: &#123; <span class="attr">id</span>: post.<span class="property">id</span> &#125;</span><br><span class="line">    &#125;))</span><br><span class="line">    <span class="comment">// 构建时只预渲染 paths 路径，blocking 在页面不存在时按需渲染。</span></span><br><span class="line">    <span class="keyword">return</span> &#123; paths, <span class="attr">fallback</span>: <span class="string">&#x27;blocking&#x27;</span> &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>


<h1 id="三、服务端渲染原理"><a href="#三、服务端渲染原理" class="headerlink" title="三、服务端渲染原理"></a>三、服务端渲染原理</h1><ul>
<li><strong>服务端渲染过程</strong>：页面访问请求、获取初始化 store、调用 renderToString 将组件渲染为 html 字符串、通过 script 添加 html 依赖并插入初始化所需的 state。</li>
<li><strong>客户端渲染过程</strong>：浏览器加载 html 显示无法交互的首屏页面、加载执行 js 而进行页面水合、浏览器渲染完成而可交互、页面跳转时向服务器请求后更新页面。</li>
</ul>
<h2 id="组件实现"><a href="#组件实现" class="headerlink" title="组件实现"></a>组件实现</h2>  <figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// ReactDOMServer.renderToString(element)：服务端渲染组件为 html 字符串</span></span><br><span class="line"><span class="keyword">import</span> <span class="title class_">React</span>, &#123; <span class="title class_">Component</span> &#125; <span class="keyword">from</span> <span class="string">&quot;react&quot;</span>;</span><br><span class="line"><span class="keyword">import</span> <span class="title class_">Header</span> <span class="keyword">from</span> <span class="string">&quot;../../components/Header&quot;</span>;</span><br><span class="line"><span class="keyword">import</span> &#123; connect &#125; <span class="keyword">from</span> <span class="string">&quot;react-redux&quot;</span>;</span><br><span class="line"><span class="keyword">import</span> &#123; getHomeList &#125; <span class="keyword">from</span> <span class="string">&quot;./store/actions&quot;</span>;</span><br><span class="line"><span class="keyword">class</span> <span class="title class_">Home</span> <span class="keyword">extends</span> <span class="title class_ inherited__">Component</span> &#123;</span><br><span class="line">    <span class="title function_">constructor</span>(<span class="params">props</span>) &#123;</span><br><span class="line">        <span class="variable language_">super</span>(props);</span><br><span class="line">        <span class="variable language_">this</span>.<span class="property">state</span> = &#123; <span class="attr">date</span>: <span class="keyword">new</span> <span class="title class_">Date</span>() &#125;;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="title function_">getList</span>(<span class="params"></span>) &#123;</span><br><span class="line">        <span class="keyword">const</span> &#123; list &#125; = <span class="variable language_">this</span>.<span class="property">props</span>;</span><br><span class="line">        <span class="keyword">return</span> list.<span class="title function_">map</span>(<span class="function"><span class="params">item</span>=&gt;</span> <span class="language-xml"><span class="tag">&lt;<span class="name">div</span> <span class="attr">key</span>=<span class="string">&#123;item.id&#125;</span>&gt;</span>&#123;item.name&#125;<span class="tag">&lt;/<span class="name">div</span>&gt;</span></span>);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">// 服务端渲染时不会执行</span></span><br><span class="line">    <span class="title function_">componentDidMount</span>(<span class="params"></span>) &#123;</span><br><span class="line">        <span class="variable language_">this</span>.<span class="property">props</span>.<span class="title function_">getHomeList</span>();</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="title function_">render</span>(<span class="params"></span>) &#123;</span><br><span class="line">        <span class="keyword">return</span> (</span><br><span class="line">            <span class="language-xml"><span class="tag">&lt;<span class="name">div</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">                <span class="tag">&lt;<span class="name">Header</span> /&gt;</span></span></span><br><span class="line"><span class="language-xml">                &#123;this.state.date.toLocaleDateString()&#125;</span></span><br><span class="line"><span class="language-xml">                &#123;this.getList()&#125;</span></span><br><span class="line"><span class="language-xml">                <span class="tag">&lt;<span class="name">button</span> <span class="attr">onClick</span>=<span class="string">&#123;()</span>=&gt;</span>&#123; alert(1); &#125;&#125;&gt;click<span class="tag">&lt;/<span class="name">button</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">            <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span></span><br><span class="line">        );</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">// 服务端执行：请求时通过 dispatch 对应 action 来初始化 store</span></span><br><span class="line"><span class="title class_">Home</span>.<span class="property">loadData</span> = <span class="function">(<span class="params">store</span>) =&gt;</span> &#123;</span><br><span class="line">    <span class="keyword">return</span> store.<span class="title function_">dispatch</span>(<span class="title function_">getHomeList</span>());</span><br><span class="line">&#125;;</span><br><span class="line"><span class="keyword">const</span> <span class="title function_">mapStateToProps</span> = (<span class="params">state</span>) =&gt; (&#123;</span><br><span class="line">    <span class="attr">list</span>: state.<span class="property">home</span>.<span class="property">newsList</span>,</span><br><span class="line">&#125;);</span><br><span class="line"><span class="comment">// 浏览器执行：页面渲染完成后通过 dispatch 对应 action 来初始化 store</span></span><br><span class="line"><span class="keyword">const</span> <span class="title function_">mapDispatchToProps</span> = (<span class="params">dispatch</span>) =&gt; (&#123;</span><br><span class="line">    <span class="title function_">getHomeList</span>(<span class="params"></span>) &#123; </span><br><span class="line">        <span class="title function_">dispatch</span>(<span class="title function_">getHomeList</span>());</span><br><span class="line">    &#125;</span><br><span class="line">&#125;);</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> <span class="title function_">connect</span>(mapStateToProps, mapDispatchToProps)(<span class="title class_">Home</span>);</span><br></pre></td></tr></table></figure>

<h2 id="渲染接口"><a href="#渲染接口" class="headerlink" title="渲染接口"></a>渲染接口</h2>  <figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 处理请求</span></span><br><span class="line"><span class="keyword">import</span> express <span class="keyword">from</span> <span class="string">&quot;express&quot;</span>;</span><br><span class="line"><span class="keyword">import</span> &#123; matchRoutes &#125; <span class="keyword">from</span> <span class="string">&quot;react-router-config&quot;</span>;</span><br><span class="line"><span class="keyword">import</span> &#123; render &#125; <span class="keyword">from</span> <span class="string">&quot;./utils&quot;</span>;</span><br><span class="line"><span class="keyword">import</span> &#123; getStore &#125; <span class="keyword">from</span> <span class="string">&quot;../store&quot;</span>;</span><br><span class="line"><span class="keyword">import</span> routes <span class="keyword">from</span> <span class="string">&quot;../Routes&quot;</span>;</span><br><span class="line"><span class="keyword">const</span> app = <span class="title function_">express</span>();</span><br><span class="line"><span class="comment">// 利用中间件将 public 设置为根目录，public/index.js == clint/index.js</span></span><br><span class="line">app.<span class="title function_">use</span>(express.<span class="title function_">static</span>(<span class="string">&quot;public&quot;</span>));</span><br><span class="line">app.<span class="title function_">get</span>(<span class="string">&quot;*&quot;</span>, <span class="keyword">function</span> (<span class="params">req, res</span>) &#123;</span><br><span class="line">    <span class="keyword">const</span> promises = [];</span><br><span class="line">    <span class="keyword">const</span> store = <span class="title function_">getStore</span>();</span><br><span class="line">    <span class="comment">// store 数据添加：路由匹配到的所有组件执行一次对应的 loadData</span></span><br><span class="line">    <span class="keyword">const</span> matchedRoutes = <span class="title function_">matchRoutes</span>(routes, req.<span class="property">path</span>);</span><br><span class="line">    matchedRoutes.<span class="title function_">forEach</span>(<span class="function">(<span class="params">item</span>) =&gt;</span> &#123;</span><br><span class="line">        <span class="keyword">if</span> (item.<span class="property">route</span>.<span class="property">loadData</span>) &#123;</span><br><span class="line">            promises.<span class="title function_">push</span>(item.<span class="property">route</span>.<span class="title function_">loadData</span>(store));</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;);</span><br><span class="line">    <span class="title class_">Promise</span>.<span class="title function_">all</span>(promises).<span class="title function_">then</span>(<span class="function">() =&gt;</span> &#123;</span><br><span class="line">        res.<span class="title function_">send</span>(<span class="title function_">render</span>(store, routes, req));</span><br><span class="line">    &#125;);</span><br><span class="line">&#125;);</span><br><span class="line"><span class="keyword">var</span> server = app.<span class="title function_">listen</span>(<span class="number">3000</span>);</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">// routes.js</span></span><br><span class="line"><span class="keyword">import</span> <span class="title class_">Home</span> <span class="keyword">from</span> <span class="string">&#x27;./containers/Home&#x27;</span>;</span><br><span class="line"><span class="keyword">import</span> <span class="title class_">Login</span> <span class="keyword">from</span> <span class="string">&#x27;./containers/Login&#x27;</span>;</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> [</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="attr">path</span>: <span class="string">&#x27;/&#x27;</span>, </span><br><span class="line">        <span class="attr">key</span>: <span class="string">&#x27;home&#x27;</span>, </span><br><span class="line">        <span class="attr">exact</span>: <span class="literal">true</span>, </span><br><span class="line">        <span class="attr">component</span>: <span class="title class_">Home</span>, </span><br><span class="line">        <span class="attr">loadData</span>: <span class="title class_">Home</span>.<span class="property">loadData</span></span><br><span class="line">    &#125;, </span><br><span class="line">    &#123;</span><br><span class="line">        <span class="attr">path</span>: <span class="string">&#x27;/login&#x27;</span>,</span><br><span class="line">        <span class="attr">key</span>: <span class="string">&#x27;login&#x27;</span>,</span><br><span class="line">        <span class="attr">exact</span>: <span class="literal">true</span>,</span><br><span class="line">        <span class="attr">component</span>: <span class="title class_">Login</span></span><br><span class="line">    &#125;</span><br><span class="line">];</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@title</span> render.js：store 更新后用来渲染组件。renderStaticMarkup </span></span><br><span class="line"><span class="comment"> * 功能类似 renderToString，但是渲染的 html 节点不会增加 data-react* </span></span><br><span class="line"><span class="comment"> * 类似的属性，它们用于服务端 html 在前端进行水合时帮助 React 避免重复的</span></span><br><span class="line"><span class="comment"> * 节点渲染而只去给节点绑定一些事件。通过 renderStaticMarkup dom 节点</span></span><br><span class="line"><span class="comment"> * 在前端水合时完全重新渲染，页面虽然会抖动一下但避免了多余属性而性能更好。</span></span><br><span class="line"><span class="comment">**/</span> </span><br><span class="line"><span class="keyword">import</span> <span class="title class_">React</span> <span class="keyword">from</span> <span class="string">&quot;react&quot;</span>;</span><br><span class="line"><span class="keyword">import</span> &#123; renderToString &#125; <span class="keyword">from</span> <span class="string">&quot;react-dom/server&quot;</span>;</span><br><span class="line"><span class="keyword">import</span> &#123; <span class="title class_">StaticRouter</span>, <span class="title class_">Route</span> &#125; <span class="keyword">from</span> <span class="string">&quot;react-router-dom&quot;</span>;</span><br><span class="line"><span class="keyword">import</span> &#123; <span class="title class_">Provider</span> &#125; <span class="keyword">from</span> <span class="string">&quot;react-redux&quot;</span>;</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> <span class="title function_">render</span> = (<span class="params">store, routes, req</span>) =&gt; &#123;</span><br><span class="line">    <span class="keyword">const</span> content = <span class="title function_">renderToString</span>(</span><br><span class="line">        <span class="language-xml"><span class="tag">&lt;<span class="name">Provider</span> <span class="attr">store</span>=<span class="string">&#123;store&#125;</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">            <span class="tag">&lt;<span class="name">StaticRouter</span> <span class="attr">location</span>=<span class="string">&#123;req.path&#125;</span> <span class="attr">context</span>=<span class="string">&#123;&#123;&#125;&#125;</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">                <span class="tag">&lt;<span class="name">div</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">                    &#123;routes.map((route) =&gt; (</span></span><br><span class="line"><span class="language-xml">                        <span class="tag">&lt;<span class="name">Route</span> &#123;<span class="attr">...route</span>&#125; /&gt;</span></span></span><br><span class="line"><span class="language-xml">                    ))&#125;</span></span><br><span class="line"><span class="language-xml">                <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">            <span class="tag">&lt;/<span class="name">StaticRouter</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">        <span class="tag">&lt;/<span class="name">Provider</span>&gt;</span></span></span><br><span class="line">    );</span><br><span class="line">    <span class="keyword">return</span> <span class="string">`</span></span><br><span class="line"><span class="string">      &lt;html&gt;</span></span><br><span class="line"><span class="string">          &lt;head&gt;&lt;title&gt;ssr&lt;/title&gt;&lt;/head&gt;</span></span><br><span class="line"><span class="string">          &lt;body&gt;</span></span><br><span class="line"><span class="string">              &lt;div id=&quot;root&quot;&gt;<span class="subst">$&#123;content&#125;</span>&lt;/div&gt;</span></span><br><span class="line"><span class="string">              &lt;script&gt;</span></span><br><span class="line"><span class="string">                  window.context = &#123;</span></span><br><span class="line"><span class="string">                      state: <span class="subst">$&#123;<span class="built_in">JSON</span>.stringify(store.getState())&#125;</span></span></span><br><span class="line"><span class="string">                  &#125;</span></span><br><span class="line"><span class="string">              &lt;/script&gt;</span></span><br><span class="line"><span class="string">              &lt;script src=&#x27;/index.js&#x27;&gt;&lt;/script&gt;</span></span><br><span class="line"><span class="string">          &lt;/body&gt;</span></span><br><span class="line"><span class="string">      &lt;/html&gt;</span></span><br><span class="line"><span class="string">    `</span>;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>


<h2 id="前端水合"><a href="#前端水合" class="headerlink" title="前端水合"></a>前端水合</h2>  <figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// hydration 水合：将服务端渲染的 html 在客户端显示的过程。</span></span><br><span class="line"><span class="keyword">import</span> <span class="title class_">React</span> <span class="keyword">from</span> <span class="string">&quot;react&quot;</span>;</span><br><span class="line"><span class="keyword">import</span> <span class="title class_">ReactDom</span> <span class="keyword">from</span> <span class="string">&quot;react-dom&quot;</span>;</span><br><span class="line"><span class="keyword">import</span> &#123; <span class="title class_">BrowserRouter</span>, <span class="title class_">Route</span> &#125; <span class="keyword">from</span> <span class="string">&quot;react-router-dom&quot;</span>;</span><br><span class="line"><span class="keyword">import</span> routes <span class="keyword">from</span> <span class="string">&quot;../Routes&quot;</span>;</span><br><span class="line"><span class="keyword">import</span> &#123; getClientStore &#125; <span class="keyword">from</span> <span class="string">&quot;../store&quot;</span>;</span><br><span class="line"><span class="keyword">import</span> &#123; <span class="title class_">Provider</span> &#125; <span class="keyword">from</span> <span class="string">&quot;react-redux&quot;</span>;</span><br><span class="line"><span class="keyword">const</span> store = <span class="title function_">getClientStore</span>();</span><br><span class="line"><span class="keyword">const</span> <span class="title function_">App</span> = (<span class="params"></span>) =&gt; &#123;</span><br><span class="line">    <span class="keyword">return</span> (</span><br><span class="line">        <span class="language-xml"><span class="tag">&lt;<span class="name">Provider</span> <span class="attr">store</span>=<span class="string">&#123;store&#125;</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">            <span class="tag">&lt;<span class="name">BrowserRouter</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">                <span class="tag">&lt;<span class="name">div</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">                    &#123;routes.map((route) =&gt; (<span class="tag">&lt;<span class="name">Route</span> &#123;<span class="attr">...route</span>&#125; /&gt;</span>))&#125;</span></span><br><span class="line"><span class="language-xml">                <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">            <span class="tag">&lt;/<span class="name">BrowserRouter</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">        <span class="tag">&lt;/<span class="name">Provider</span>&gt;</span></span></span><br><span class="line">    );</span><br><span class="line">&#125;;</span><br><span class="line"><span class="title class_">ReactDom</span>.<span class="title function_">hydrate</span>(<span class="language-xml"><span class="tag">&lt;<span class="name">App</span> /&gt;</span></span>, <span class="variable language_">document</span>.<span class="title function_">getElementById</span>(<span class="string">&quot;root&quot;</span>));</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">// getClientStore：客户端使用的 store</span></span><br><span class="line"><span class="keyword">import</span> &#123; createStore, applyMiddleware, combineReducers &#125; <span class="keyword">from</span> <span class="string">&#x27;redux&#x27;</span>;</span><br><span class="line"><span class="keyword">import</span> thunk <span class="keyword">from</span> <span class="string">&#x27;redux-thunk&#x27;</span>;</span><br><span class="line"><span class="keyword">import</span> &#123; reducer <span class="keyword">as</span> homeReducer &#125; <span class="keyword">from</span> <span class="string">&#x27;../containers/Home/store&#x27;</span>;</span><br><span class="line"><span class="keyword">const</span> reducer = <span class="title function_">combineReducers</span>(&#123; <span class="attr">home</span>: homeReducer &#125;);</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> <span class="title function_">getStore</span> = (<span class="params"></span>) =&gt; &#123;  <span class="comment">// 使用函数避免共用一个实例</span></span><br><span class="line">    <span class="keyword">return</span> <span class="title function_">createStore</span>(reducer, <span class="title function_">applyMiddleware</span>(thunk));</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> <span class="title function_">getClientStore</span> = (<span class="params"></span>) =&gt; &#123;</span><br><span class="line">    <span class="keyword">const</span> initState = <span class="variable language_">window</span>.<span class="property">context</span>.<span class="property">state</span>;</span><br><span class="line">    <span class="keyword">return</span> <span class="title function_">createStore</span>(reducer, initState, <span class="title function_">applyMiddleware</span>(thunk));</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>





    </div>

    
    
    <div>
      
        <div>
    
        <div style="text-align:center;color: #ccc;font-size:14px;margin-top:20px;">-------------本文结束<i class="fa fa-paw"></i>感谢您的阅读-------------</div>
    
</div>

      
    </div>
    
        

<div>
<ul class="post-copyright">
  <li class="post-copyright-author">
    <strong>本文作者： </strong>chuangges
  </li>
  <li class="post-copyright-link">
    <strong>本文链接：</strong>
    <a href="https://chuangges.gitee.io/2023/09/04/23-next%E9%A6%96%E5%B1%8F%E6%B8%B2%E6%9F%93/" title="最强首屏渲染方案 NextJS">https://chuangges.gitee.io/2023/09/04/23-next首屏渲染/</a>
  </li>
  <li class="post-copyright-license">
    <strong>版权声明： </strong>本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/zh_CN" rel="external nofollow noopener noreferrer" target="_blank"><i class="fab fa-fw fa-creative-commons"></i>BY-NC-SA</a> 许可协议。转载请注明出处！
  </li>
</ul>
</div>


      <footer class="post-footer">

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/2023/08/14/22-%E4%BD%8E%E4%BB%A3%E7%A0%81%E5%B9%B3%E5%8F%B0/" rel="prev" title="低代码平台开发">
      <i class="fa fa-chevron-left"></i> 低代码平台开发
    </a></div>
      <div class="post-nav-item">
    <a href="/2023/12/04/24-web%E7%BC%93%E5%AD%98/" rel="next" title="Web 缓存机制">
      Web 缓存机制 <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#%E4%B8%80%E3%80%81%E5%9F%BA%E7%A1%80%E5%85%A5%E9%97%A8"><span class="nav-number">1.</span> <span class="nav-text">一、基础入门</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%A0%B8%E5%BF%83%E6%9C%BA%E5%88%B6"><span class="nav-number">1.1.</span> <span class="nav-text">核心机制</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%9E%84%E5%BB%BA%E9%A1%B9%E7%9B%AE"><span class="nav-number">1.2.</span> <span class="nav-text">构建项目</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E9%9B%86%E6%88%90-Node"><span class="nav-number">1.3.</span> <span class="nav-text">集成 Node</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%A0%B7%E5%BC%8F%E6%96%B9%E6%A1%88"><span class="nav-number">1.4.</span> <span class="nav-text">样式方案</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%85%AC%E5%85%B1%E7%BB%84%E4%BB%B6"><span class="nav-number">1.5.</span> <span class="nav-text">公共组件</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%8A%A8%E6%80%81%E9%A1%B5%E9%9D%A2"><span class="nav-number">1.6.</span> <span class="nav-text">动态页面</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E8%B7%AF%E7%94%B1%E6%9C%BA%E5%88%B6"><span class="nav-number">1.7.</span> <span class="nav-text">路由机制</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%BC%82%E6%AD%A5%E5%8A%A0%E8%BD%BD"><span class="nav-number">1.8.</span> <span class="nav-text">异步加载</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%95%B0%E6%8D%AE%E8%8E%B7%E5%8F%96"><span class="nav-number">1.9.</span> <span class="nav-text">数据获取</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%89%A9%E5%B1%95%E5%8A%9F%E8%83%BD"><span class="nav-number">1.10.</span> <span class="nav-text">扩展功能</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E4%BA%8C%E3%80%81%E6%B8%B2%E6%9F%93%E6%A8%A1%E5%BC%8F"><span class="nav-number">2.</span> <span class="nav-text">二、渲染模式</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#CSR-%E5%AE%A2%E6%88%B7%E7%AB%AF%E6%B8%B2%E6%9F%93"><span class="nav-number">2.1.</span> <span class="nav-text">CSR 客户端渲染</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#SSR-%E6%9C%8D%E5%8A%A1%E7%AB%AF%E6%B8%B2%E6%9F%93"><span class="nav-number">2.2.</span> <span class="nav-text">SSR 服务端渲染</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#SSG-%E9%9D%99%E6%80%81%E9%A2%84%E6%B8%B2%E6%9F%93"><span class="nav-number">2.3.</span> <span class="nav-text">SSG 静态预渲染</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#ISR-%E5%A2%9E%E9%87%8F%E6%B8%B2%E6%9F%93"><span class="nav-number">2.4.</span> <span class="nav-text">ISR 增量渲染</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E4%B8%89%E3%80%81%E6%9C%8D%E5%8A%A1%E7%AB%AF%E6%B8%B2%E6%9F%93%E5%8E%9F%E7%90%86"><span class="nav-number">3.</span> <span class="nav-text">三、服务端渲染原理</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E7%BB%84%E4%BB%B6%E5%AE%9E%E7%8E%B0"><span class="nav-number">3.1.</span> <span class="nav-text">组件实现</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%B8%B2%E6%9F%93%E6%8E%A5%E5%8F%A3"><span class="nav-number">3.2.</span> <span class="nav-text">渲染接口</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%89%8D%E7%AB%AF%E6%B0%B4%E5%90%88"><span class="nav-number">3.3.</span> <span class="nav-text">前端水合</span></a></li></ol></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="chuangges" src="" data-original="/images/chuang.png">
  <p class="site-author-name" itemprop="name">chuangges</p>
  <div class="site-description" itemprop="description">chuangge's blog.</div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">39</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/categories/">
          
        <span class="site-state-item-count">7</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/tags/">
          
        <span class="site-state-item-count">7</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author motion-element">
      <span class="links-of-author-item">
        <a href="https://github.com/chuangges" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;chuangges" rel="external nofollow noopener noreferrer" target="_blank"><i class="fab fa-github fa-fw"></i>GitHub</a>
      </span>
      <span class="links-of-author-item">
        <a href="https://stackoverflow.com/yourname" title="StackOverflow → https:&#x2F;&#x2F;stackoverflow.com&#x2F;yourname" rel="external nofollow noopener noreferrer" target="_blank"><i class="fab fa-stack-overflow fa-fw"></i>StackOverflow</a>
      </span>
  </div>


  <div class="links-of-blogroll motion-element">
    <div class="links-of-blogroll-title"><i class="fa fa-link fa-fw"></i>
      友情链接
    </div>
    <ul class="links-of-blogroll-list">
        <li class="links-of-blogroll-item">
          <a href="https://juejin.im/" title="https:&#x2F;&#x2F;juejin.im&#x2F;" rel="external nofollow noopener noreferrer" target="_blank">掘金</a>
        </li>
        <li class="links-of-blogroll-item">
          <a href="https://www.nowcoder.com/" title="https:&#x2F;&#x2F;www.nowcoder.com&#x2F;" rel="external nofollow noopener noreferrer" target="_blank">牛客网</a>
        </li>
        <li class="links-of-blogroll-item">
          <a href="https://www.docschina.org/" title="https:&#x2F;&#x2F;www.docschina.org&#x2F;" rel="external nofollow noopener noreferrer" target="_blank">印记中文</a>
        </li>
        <li class="links-of-blogroll-item">
          <a href="http://tool.lu/" title="http:&#x2F;&#x2F;tool.lu&#x2F;" rel="external nofollow noopener noreferrer" target="_blank">在线工具</a>
        </li>
        <li class="links-of-blogroll-item">
          <a href="https://tinypng.com/" title="https:&#x2F;&#x2F;tinypng.com&#x2F;" rel="external nofollow noopener noreferrer" target="_blank">图片压缩</a>
        </li>
        <li class="links-of-blogroll-item">
          <a href="https://bigjpg.com/" title="https:&#x2F;&#x2F;bigjpg.com&#x2F;" rel="external nofollow noopener noreferrer" target="_blank">图片放大</a>
        </li>
        <li class="links-of-blogroll-item">
          <a href="https://tool.chinaz.com/" title="https:&#x2F;&#x2F;tool.chinaz.com&#x2F;" rel="external nofollow noopener noreferrer" target="_blank">站长工具</a>
        </li>
        <li class="links-of-blogroll-item">
          <a href="https://learn-anything.xyz/" title="https:&#x2F;&#x2F;learn-anything.xyz&#x2F;" rel="external nofollow noopener noreferrer" target="_blank">Learn-Anything</a>
        </li>
        <li class="links-of-blogroll-item">
          <a href="https://stackoverflow.com/" title="https:&#x2F;&#x2F;stackoverflow.com&#x2F;" rel="external nofollow noopener noreferrer" target="_blank">Stack-Overflow</a>
        </li>
    </ul>
  </div>

      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2024</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">chuangge</span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-chart-area"></i>
    </span>
    <span title="站点总字数">1m</span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-coffee"></i>
    </span>
    <span title="站点阅读时长">15:14</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" class="theme-link" rel="external nofollow noopener noreferrer" target="_blank">Hexo</a> & <a href="https://pisces.theme-next.org/" class="theme-link" rel="external nofollow noopener noreferrer" target="_blank">NexT.Pisces</a> 强力驱动
  </div>

        








      </div>
    </footer>
  </div>

  
  <script src="/lib/anime.min.js"></script>
  <script src="/lib/velocity/velocity.min.js"></script>
  <script src="/lib/velocity/velocity.ui.min.js"></script>

<script src="/js/utils.js"></script>

<script src="/js/motion.js"></script>


<script src="/js/schemes/pisces.js"></script>


<script src="/js/next-boot.js"></script>




  















  

  

<script src="/live2dw/lib/L2Dwidget.min.js?094cbace49a39548bed64abff5988b05"></script><script>L2Dwidget.init({"pluginRootPath":"live2dw/","pluginJsPath":"lib/","pluginModelPath":"assets/","model":{"jsonPath":"/live2dw/assets/tororo.model.json"},"display":{"position":"left","width":180,"height":360},"mobile":{"show":false},"react":{"opacityDefault":1,"opacityOnHover":1},"log":false,"tagMode":false});</script>
        <style>
            [bg-lazy] {
                background-image: none !important;
                background-color: #eee !important;
            }
        </style>
        <script>
            window.imageLazyLoadSetting = {
                isSPA: false,
                preloadRatio: 1,
                processImages: null,
            };
        </script><script>window.addEventListener("load",function(){var t=/\.(gif|jpg|jpeg|tiff|png)$/i,r=/^data:image\/[a-z]+;base64,/;Array.prototype.slice.call(document.querySelectorAll("img[data-original]")).forEach(function(a){var e=a.parentNode;"A"===e.tagName&&(e.href.match(t)||e.href.match(r))&&(e.href=a.dataset.original)})});</script><script>!function(e){e.imageLazyLoadSetting.processImages=t;var n=e.imageLazyLoadSetting.isSPA,i=e.imageLazyLoadSetting.preloadRatio||1,r=o();function o(){var t=Array.prototype.slice.call(document.querySelectorAll("img[data-original]")),e=Array.prototype.slice.call(document.querySelectorAll("[bg-lazy]"));return t.concat(e)}function t(){n&&(r=o());for(var t,a=0;a<r.length;a++)0<=(t=(t=r[a]).getBoundingClientRect()).bottom&&0<=t.left&&t.top<=(e.innerHeight*i||document.documentElement.clientHeight*i)&&function(){var t,e,n,i,o=r[a];e=function(){r=r.filter(function(t){return o!==t})},(t=o).hasAttribute("bg-lazy")?(t.removeAttribute("bg-lazy"),e&&e()):(n=new Image,i=t.getAttribute("data-original"),n.onload=function(){t.src=i,t.removeAttribute("data-original"),e&&e()},t.src!==i&&(n.src=i))}()}function a(){clearTimeout(t.tId),t.tId=setTimeout(t,500)}t(),document.addEventListener("scroll",a),e.addEventListener("resize",a),e.addEventListener("orientationchange",a)}(this);</script></body>
</html>
